package com.ityuwen.mengyagushi.user.api;

import com.ityuwen.mengyagushi.common.login.GuiGuLogin;
import com.ityuwen.mengyagushi.common.result.Result;
import com.ityuwen.mengyagushi.common.util.AuthContextHolder;
import com.ityuwen.mengyagushi.model.user.UserInfo;
import com.ityuwen.mengyagushi.user.service.UserInfoService;
import com.ityuwen.mengyagushi.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("wxLogin/{code}")
    public Result<Map<String, Object>> wxLogin(@PathVariable("code")String code, @RequestParam(value = "type", defaultValue = "2")String type) throws FileNotFoundException {
        Map<String, Object> map = this.userInfoService.wxLogin(code, type);
        return Result.ok(map);
    }

    @GuiGuLogin
    @GetMapping("getUserInfo")
    public Result<UserInfoVo> getUserInfo(){
        Long userId = AuthContextHolder.getUserId();
        UserInfo userInfo = this.userInfoService.getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return Result.ok(userInfoVo);
    }

    @GuiGuLogin
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo){
        Long userId = AuthContextHolder.getUserId();
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoVo, userInfo);
        userInfo.setId(userId);
        this.userInfoService.updateById(userInfo);
        return Result.ok();
    }
}
